//
//  AdminReportViewController.m
//  WeCommunity
//
//  Created by mac on 2017/7/18.
//  Copyright © 2017年 lzy. All rights reserved.
//

#import "AdminReportViewController.h"
#import "DynamicModel.h"
#import "DynamicCell.h"
#import "PersonalHomePageViewController.h"
#import "CWebViewController.h"

@interface AdminReportViewController ()<UITableViewDelegate,UITableViewDataSource,DynamicClickContentDelegate>

@property (nonatomic,strong) UIBarButtonItem * rightBtnItem;
@property (nonatomic,strong) UITableView * tableView;
@property (nonatomic,strong) NSMutableArray * dataSource;
@property (nonatomic,strong) MJRefreshAutoGifFooter * footer;
@property (nonatomic,strong) CustomRefreshHeader * refreshHeader;

@property (nonatomic,assign) BOOL isPull;


@end

@implementation AdminReportViewController

-(UIBarButtonItem *)rightBtnItem
{
    if (!_rightBtnItem) {
        _rightBtnItem = [[UIBarButtonItem alloc] initWithTitle:KNSLocal(@"删除全部") style:UIBarButtonItemStylePlain target:self action:@selector(deleteAllReportDynamic)];
        [_rightBtnItem setTitleTextAttributes:@{NSFontAttributeName:PFR_FONT(16), NSForegroundColorAttributeName :TINT_GREEN_COLOR} forState:UIControlStateNormal];
    }
    return _rightBtnItem;
}

-(UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, KScreen_Width, KScreen_Height - KNaviheight) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = WHITE_COLOR;
        
        @weakify(self);
        self.refreshHeader = [CustomRefreshHeader headerWithRefreshingBlock:^{
            @strongify(self);
            [self headerRefreshData];
        }];
        _tableView.mj_header = self.refreshHeader;
        
        self.footer = [MJRefreshAutoGifFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerRefreshData)];
        [self.footer setTitle:@"" forState:MJRefreshStateIdle];
        [self.footer setTitle:@"" forState:MJRefreshStateRefreshing];
        _tableView.mj_footer = _footer;
        
    }
    return _tableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initWithUserInterface];
    
    self.dataSource = [NSMutableArray array];
    
    [self.refreshHeader beginRefreshing];

    [self setDeleteAllItemIsOk:NO];
}

-(void)initWithUserInterface
{
    self.title = KNSLocal(@"收到的举报内容");
    
    self.navigationItem.rightBarButtonItem = self.rightBtnItem;
    
    [self.view addSubview:self.tableView];
}
#pragma mark - 下拉刷新数据
-(void)headerRefreshData
{
    self.isPull = YES;
    [self loadingData];
}
#pragma mark - 上拉加载数据
-(void)footerRefreshData
{
    self.isPull = NO;
    [self loadingData];
}
#pragma mark - 加载数据
-(void)loadingData
{
    NSDate * loadDate = nil;
    if (!self.isPull && self.dataSource > 0) {
        loadDate = ((DynamicModel *)self.dataSource.lastObject).createDate;
    }
    
    [[DynamicServer shared] adminLoadDynamicDataWithDate:loadDate
                                              loadCount:30
                                               complete:^(BOOL success, NSString *error, NSArray *dataArray) {
                                                   if (success) {
                                                       if (self.isPull) {
                                                           [self.dataSource removeAllObjects];
                                                       }
                                                       [self.dataSource addObjectsFromArray:dataArray];
                                                       
                                                       if (dataArray.count == 0) {
                                                           [self.footer setTitle:KHasLoadedAllTitle forState:MJRefreshStateIdle];
                                                           [self.footer setTitle:KHasLoadedAllTitle forState:MJRefreshStateRefreshing];
                                                       }else{
                                                           [self.footer setTitle:KDragOrClickToDragTitle forState:MJRefreshStateIdle];
                                                           [self.footer setTitle:KLoaddingMoreTitle forState:MJRefreshStateRefreshing];
                                                       }
                                                   }else{
                                                       [UITool showHudWithText:error withView:self.view];
                                                   }
                                                   [self endRefreshAnimation:success];
                                               }];
}

-(void)endRefreshAnimation:(BOOL)success
{
    if (success) {
        [(CustomRefreshHeader*)self.tableView.mj_header endRefreshingSuccess];
    }else{
        [(CustomRefreshHeader*)self.tableView.mj_header endRefreshingFail];
    }
    [self.tableView.mj_footer endRefreshing];
    [self.tableView reloadData];
    
    if (self.dataSource.count > 0) {
        [self setDeleteAllItemIsOk:YES];
    }else{
        [self setDeleteAllItemIsOk:NO];
    }
}

#pragma mark - tableView delegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSource.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    DynamicModel *  model = self.dataSource[indexPath.row];
    return model.totalHeight;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    DynamicModel *  model= self.dataSource[indexPath.row];
    
    static NSString * cellID = @"dynamicCell";
    DynamicCell * cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell =[[DynamicCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        cell.delegate = self;
    }
    [cell creatCellWithModel:model showDelete:YES showReportCount:YES];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    DynamicModel * model= self.dataSource[indexPath.row];
    
    NSLog(@"%@",model);
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        DynamicModel * model= self.dataSource[indexPath.row];
        
        [self showLoadingHudWithHText:nil];
        [self setDeleteAllItemIsOk:NO];
        @weakify(self);
        [[DynamicServer shared] deleteBeenReportedDynamicWithObjectId:model.objectId complete:^(BOOL success, NSString *error) {
            @strongify(self);
            [self hiddenLoadingHud];
            
            if (success) {
                [self.dataSource removeObject:model];
                [self.tableView reloadData];
                
                if (self.dataSource.count > 0) {
                    [self setDeleteAllItemIsOk:YES];
                }else{
                    [self setDeleteAllItemIsOk:NO];
                }
            }else{
                [UITool showHudWithText:error withView:self.view];
                [self setDeleteAllItemIsOk:YES];
            }
        }];
    }
}
// 修改编辑按钮文字
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return KNSLocal(@"deleteTitle");
}


#pragma mark - tableviewCell delegate
#pragma mark - 点赞.举报.more
-(void)likeDynamicWithSender:(UIView *)sender clickCell:(DynamicCell *)cell
{
    NSIndexPath *cellIndexPath = [self.tableView indexPathForCell:cell];
    DynamicModel * model = self.dataSource[cellIndexPath.row];
    if (sender.tag == LikeBtn_tag) { //点赞
        [[DynamicServer shared] likeOrUnLikeDataWithDynamic:model complete:^(BOOL success, NSString *error, DynamicModel *model) {
            if (success) {
                [cell changeLikeNumWithDynamic:model];
            }else{
                [UITool showHudWithText:error withView:self.view];
            }
        }];
    }else if (sender.tag == ReportBtn_tag){ //删除

        [self deleteMineDynamic:model];
        
    }else if (sender.tag == ContactBtn_tag){ // 点击用户头像
        
        PersonalHomePageViewController * homePageVC = [[PersonalHomePageViewController alloc] init];
        homePageVC.contact = model.contact;
        [self.navigationController pushViewController:homePageVC animated:YES];
    }
}
#pragma mark - 显示全部、隐藏
-(void)isShowMoreContent:(BOOL)showMore withCell:(DynamicCell *)cell sender:(UIButton *)sender
{
    NSIndexPath *cellIndexPath = [self.tableView indexPathForCell:cell];
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.tableView reloadRowsAtIndexPaths:@[cellIndexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    });
    
}

#pragma mark - 点击文字中的链接
-(void)processCommunityUrlLink:(NSString *)link cell:(DynamicCell *)cell
{
    CWebViewController * webVC = [[CWebViewController alloc] init];
    webVC.linkUrl = link;
    [self.navigationController pushViewController:webVC animated:YES];
}


#pragma mark - 删除发的动态
-(void)deleteMineDynamic:(DynamicModel *)model
{
    [self showLoadingHudWithHText:nil];
    [self setDeleteAllItemIsOk:NO];
    @weakify(self);
    [[DynamicServer shared] deleteDynamicWithObjectId:model.objectId complete:^(BOOL success, NSString *error) {
        @strongify(self);
        [self hiddenLoadingHud];
        
        if (success) {
            [[NSNotificationCenter defaultCenter] postNotificationName:DELETE_CONTENT_NOTIKEY object:model.objectId];

            [self.dataSource removeObject:model];
            [self.tableView reloadData];
            
            if (self.dataSource.count > 0) {
                [self setDeleteAllItemIsOk:YES];
            }else{
                [self setDeleteAllItemIsOk:NO];
            }
        }else{
            [UITool showHudWithText:error withView:self.view];
            [self setDeleteAllItemIsOk:YES];
        }
    }];
}

-(void)deleteAllReportDynamic
{
    
    [self showLoadingHudWithHText:nil];
    [self setDeleteAllItemIsOk:NO];
    @weakify(self);
    [[DynamicServer shared] deleteDynamicWithObjects:self.dataSource complete:^(BOOL success, NSString *error) {
        @strongify(self);
        [self hiddenLoadingHud];
        if (success) {
            for (DynamicModel * model in self.dataSource) {
                [[NSNotificationCenter defaultCenter] postNotificationName:DELETE_CONTENT_NOTIKEY object:model.objectId];
            }
            [self.dataSource removeAllObjects];
            [self.tableView reloadData];
        }else{
            [UITool showHudWithText:error withView:self.view];
            [self setDeleteAllItemIsOk:YES];
        }
    }];
}

-(void)setDeleteAllItemIsOk:(BOOL)ok
{
    if (ok) {
        self.rightBtnItem.enabled = YES;
        [self.rightBtnItem setTitleTextAttributes:@{NSFontAttributeName:PFR_FONT(16), NSForegroundColorAttributeName :TINT_GREEN_COLOR} forState:UIControlStateNormal];
    }else{
        self.rightBtnItem.enabled = NO;
        [self.rightBtnItem setTitleTextAttributes:@{NSFontAttributeName:PFR_FONT(16), NSForegroundColorAttributeName :TINT_GREEN_TR_COLOR} forState:UIControlStateNormal];
    }
}

-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    UIMenuController * menu = [UIMenuController sharedMenuController];
    [menu setMenuVisible:NO animated:YES];
}



@end
